//
//  PopoverAnimator.swift
//  CLWeiBo
//
//  Created by 程雷 on 15/6/28.
//  Copyright © 2015年 程雷. All rights reserved.
//

import UIKit

class PopoverAnimator: NSObject,UIViewControllerTransitioningDelegate,UIViewControllerAnimatedTransitioning  {
    
    /// 展现视图的大小
    var presentFrame = CGRectZero
    /// 是否展现动画标志
    var isPresented = false
    
    // 返回负责专场的控制器对象
    func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? {
        
        let pc = PopoverPresentationController(presentedViewController: presented, presentingViewController: presenting)
        pc.presentFrame = presentFrame
        return pc
    }
    // 返回提供Modal 动画的对象
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        //展现
        isPresented = true
        return self
    }
    
    // 返回 Dismissed 动画对象
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresented = false
        return self
    }
    //动画时长
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        
        return isPresented ? 1.2:0.5
    }
    /// 转场动画实现函数 －－ 一旦实现此函数 ，默认的动画失效，所有动画由程序猿提供
    ///
    ///  :param: transitionContext 转场上下文，提供了转场动画所需的左右参数
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        
        //判断是展现还是  dismis
        
        if isPresented
        {
            let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!
            // 1.将 toview 添加到 容器视图
            transitionContext.containerView()?.addSubview(toView)
            
            //动画实现
            toView.transform = CGAffineTransformMakeScale(1.0, 0)
            toView.layer.anchorPoint = CGPoint(x: 0.5, y: 0)
            
            // initialSpringVelocity 跳跃速度
            UIView.animateWithDuration(transitionDuration(transitionContext), delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10.0, options: UIViewAnimationOptions.CurveEaseIn, animations: { () -> Void in
                toView.transform = CGAffineTransformIdentity
                
                }, completion: { (_) -> Void in
                    //结束动画 －－告诉系统转场动画已经结束
                    transitionContext.completeTransition(true)
            })
        }
            
        else
        {
            // 1. 取消 －fromview 删除
            let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
            // 2.结束动画，告诉系统转场动画已经结束
            UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
                //设置一个非常小的值就能够播放动画了
                fromView.transform = CGAffineTransformMakeScale(1.0, 0.0001)
                }, completion: { (_) -> Void in
                    // 删除动画
                    fromView.removeFromSuperview()
                    // *** 结束动画 *** 告诉系统转场动画已经结束
                    transitionContext.completeTransition(true)
            })
            
        }
    }
}
